<?php

class Publicity extends Controller {

    function Publicity()
    {
        parent::Controller();    
        if(!admin_session_check()) redirect('admin/login');
        if(!is_allowed("all")) show_404();
        $this->load->model('publicitymodel');  
        $this->load->library("form_validation");       
    }     
    
    function index()
    {

        $data["list"] = $this->publicitymodel->admin_get_list();
        $data["response"] = get_response();        

        $this->load->view('admin/default/head');  
        $this->load->view('admin/default/header', array('selected'=>'pages'));  
        $this->load->view("admin/default/nav_pages", array('selected' => 'publicity'));
        $this->load->view("admin/default/quick_search");
        $this->load->view('admin/publicity/publicity_list', $data);   
        $this->load->view('admin/default/footer');   

    }
 
    function edit($id = NULL)
    {
        if($id == NULL) show_404();

        $data["item"] = $this->publicitymodel->admin_get_item($id); 
        if(empty($data["item"])) show_404();

        $this->_process_form();

        $data["action"] = "update";
        $data["token"] = generate_token("publicity",720);

        $this->load->view('admin/default/head');  
        $this->load->view('admin/default/header', array('selected'=>'pages'));  
        $this->load->view("admin/default/nav_pages", array('selected' => 'publicity'));
        $this->load->view('admin/publicity/publicity_form', $data);   
        $this->load->view('admin/default/footer');   

    }
       
    function add()
    {
        $this->_process_form();

        $data["item"] = array(
                "id_Publicity" => "",
                "Date_Publicity" => "", 
                "Title_Publicity" => "", 
                "Introduction_Publicity" => "",
                "Description_Publicity" => "",
                "Image_Publicity" => ""
        ); 

        $data["action"] = "create";
        $data["token"] = generate_token("publicity",720);

        $this->load->view('admin/default/head');  
        $this->load->view('admin/default/header', array('selected'=>'pages'));  
        $this->load->view("admin/default/nav_pages", array('selected' => 'publicity'));
        $this->load->view('admin/publicity/publicity_form', $data);   
        $this->load->view('admin/default/footer');   

    }            
    function remove($id = false){

        if($id == NULL) show_404();  

        $data["item"] = $this->publicitymodel->admin_get_item($id, "id_Publicity as id, Date_Publicity as date, Title_Publicity as title, Introduction_Publicity as intro, Description_Publicity as description, Image_Publicity as image, Approved_Publicity as approved, Active_Publicity as active ");

        if(empty($data["item"])){

            set_response('error', "The item you're attempting to delete does not exist");
            redirect('admin/publicity');

        } else {

            $data["controller"] = "publicity";
            $this->load->model('glossarymodel');

            $this->load->view('admin/default/head');          
            $this->load->view('admin/default/header', array('selected'=>'pages'));  
            $this->load->view("admin/default/nav_pages", array('selected' => 'publicity'));
            $this->load->view('admin/publicity/publicity_delete', $data);   
            $this->load->view('admin/default/footer');   

        }
    }  
    function preview($id = NULL)
    {

        if($this->input->post("submit_action") === FALSE){

            $data["item"] = $this->publicitymodel->admin_get_item($id, "id_Publicity as id, Date_Publicity as date, Title_Publicity as title, Introduction_Publicity as intro, Description_Publicity as description, Image_Publicity as image, Approved_Publicity as approved, Active_Publicity as active "); 
            if (empty($data["item"])) show_404();             

            $data["action"] = "view";

        } else {

            $data["item"] = array(
                'id' => $this->input->post("id", TRUE),
                'date' => $this->input->post("date", TRUE),  
                'title' => $this->input->post("title", TRUE),  
                'intro' => $this->input->post("introduction", TRUE),
                'description' => $this->input->post("description", TRUE),
                'image' => $this->input->post("image", TRUE));

            $data["action"] = "preview";

        }

        $data["controller"] = "publicity";
        $data["image_folder"] = base_url().UPLOADS_FOLDER.PUBLICITY_IMG_FOLDER;			
        $this->load->model('glossarymodel');
        $this->load->view('admin/previews/preview_article_template', $data);   

    }
    function confirm_removal($id = false){

        if($id == NULL) show_404();  

        $data["item"] = $this->publicitymodel->admin_get_item($id);

        if(empty($data["item"])){

            set_response('error', "The item you're attempting to delete does not exist");
            redirect('admin/publicity');

        } else {

            $this->publicitymodel->admin_delete($id);
            set_response('success', "The item <i>".$data["item"]["Title_Publicity"]."</i> was deleted.");
            redirect('admin/publicity');
        }
    }
    function deactivate($id = false){

        if($id == NULL) show_404();  
        $data = array('Active_Publicity' => 0);
        $this->publicitymodel->admin_update($id, $data);
        $item = $this->publicitymodel->admin_get_item($id);
        set_response('success', "The item <i>".$item["Title_Publicity"].'</i> is now inactive. <a href="'.site_url("admin/publicity/preview/".$item["id_Publicity"]).'" rel="view">Click to view</a>');
        redirect('admin/publicity');
    }
    function activate($id = false){

        if($id == NULL) show_404();  
        $data = array('Active_Publicity' => 1);
        $this->publicitymodel->admin_update($id, $data);
        $item = $this->publicitymodel->admin_get_item($id);
        set_response('success', "The item <i>".$item["Title_Publicity"].'</i> is now active. <a href="'.site_url("admin/publicity/preview/".$item["id_Publicity"]).'" rel="view">Click to view</a>');
        redirect('admin/publicity');
    }
    function approve($id = false){

        if(!is_allowed(array(ROLE_ADMIN,ROLE_PUBLISHER))) show_404();
        if($id == NULL) show_404();  

        $data = array('Approved_Publicity' => 1, 'Active_Publicity' => 1);
        $this->publicitymodel->admin_update($id, $data);
        $item = $this->publicitymodel->admin_get_item($id);
        set_response('success', "The item <i>".$item["Title_Publicity"].'</i> was approved. <a href="'.site_url("admin/publicity/preview/".$item["id_Publicity"]).'" rel="view">Click to view</a>');
        redirect('admin/publicity');
    }
    function _process_form()
    {        
        if($this->input->post("submit_action") !== FALSE){

            if ($this->_validate_form()) {

                if (validate_token("publicity",$this->input->post("token",TRUE))){

                    $data = array(
                        'Date_Publicity' => $this->input->post("date"),
                        'Title_Publicity' => $this->input->post("title"),  
                        'Introduction_Publicity' => $this->input->post("introduction"),
                        'Description_Publicity' => $this->input->post("description"),
                        'Image_Publicity' => $this->input->post("image"),
                    );

                    switch($this->input->post("submit_action")){    

                        case 'create':

                            $new_id = $this->publicitymodel->admin_insert($data);
                            set_response('success','The item <i>'.$data['Title_Publicity'].'</i> was created. <a href="'.site_url("admin/publicity/preview/".$new_id).'" rel="view">Click to view</a>');

                        break;
                        case 'update':

                            $this->publicitymodel->admin_update($this->input->post("id"), $data);
                            set_response('success','The item <i>'.$data['Title_Publicity'].'</i> was updated. <a href="'.site_url("admin/publicity/preview/".$this->input->post("id")).'" rel="view">Click to view</a>');

                        break;        

                    }

                    redirect("admin/publicity");

                }
            }   
        }
    }

    private function _validate_form(){
        
        $config = array( 
                        array(
                         'field'   => 'date',
                         'label'   => 'Date',
                         'rules'   => 'trim|required|xss_clean'
                        ),
                        array(
                         'field'   => 'title',
                         'label'   => 'Title',
                         'rules'   => 'trim|required|xss_clean|max_length[250]'
                        ),
                        array(
                         'field'   => 'introduction',
                         'label'   => 'Introduction',
                         'rules'   => 'trim|required|xss_clean'
                        ),
                        array(
                         'field'   => 'description',
                         'label'   => 'Description',
                         'rules'   => 'trim|required|xss_clean'
                        ),             
                        array(
                         'field'   => 'image',
                         'label'   => 'Image',
                         'rules'   => 'trim|xss_clean'
                        )
                    );
        
                                                 
        $this->form_validation->set_rules($config);       
        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
        
        if ($this->form_validation->run($this) == FALSE){
            return FALSE;
        }else
            return TRUE;

    }

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */